package com.unb.transportebsb;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.unb.util.Util;

public class ProcurarLinhaActivity extends Activity {
    private List<String> linhas;
    List<String[]> list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.procurar_linha);

        linhas = new ArrayList<String>();

        list = Util.getPostgisLayerLn().qryAllLine();
        if (list == null) {
            finish();
            return;
        }
        for (String[] aux : list) {
            linhas.add(aux[1]);
        }

        ArrayAdapter<String> ad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, android.R.id.text1, linhas);
        ListView lv = (ListView) findViewById(R.id.list);
        lv.setAdapter(ad);

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                Object o = arg0.getItemAtPosition(position);
                String lstrEstado = o.toString();

                Toast.makeText(ProcurarLinhaActivity.this, "Voce clicou : " + lstrEstado + " - " + list.get(position)[0],
                        Toast.LENGTH_LONG).show();
                insereLinha(list.get(position)[0]);

            }

            private void insereLinha(String index) {
                Util.getPostgisLayerLn().recuperaLinhaEspecifica(Integer.parseInt(index));
                if (Util.getPostgisLayerLn() == null || Util.getPostgisLayerPtParada() == null
                        || !Util.containsLayer(Util.getPostgisLayerPtParada()) || !Util.containsLayer(Util.getPostgisLayerLn())) {
                    Util.addLayerPtLn();
                }
                finish();

            }
        });
    }
}
